package com.freak.wanandroid.model.myself.activity.collect;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.freak.wanandroid.R;
import com.freak.wanandroid.base.activity.BaseLifecycleActivity;
import com.freak.wanandroid.bean.CollectBean;
import com.freak.wanandroid.model.login.LoginActivity;
import com.freak.wanandroid.model.myself.activity.collect.adapter.CollectAdapter;
import com.freak.wanandroid.model.myself.activity.collect.detail.CollectDetailActivity;
import com.freak.wanandroid.net.CommonLiveData;
import com.freak.wanandroid.util.ToastUtil;
import com.freak.wanandroid.util.ToolUtils;
import com.freak.wanandroid.view.pullrefreshview.layout.PullRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏
 * Created by 74099 on 2018/8/29.
 */

public class CollectActivity extends BaseLifecycleActivity<CollectViewModel> {
    private List<CollectBean.Datas> mDatas;
    private int mCurPage = 1;
    private PullRefreshLayout mCollectIncludeNoData;
    private XRecyclerView mCollectRecycle;
    private CollectAdapter mCollectAdapter;
    private List<CollectBean.Datas> mListBeans;

    public static void startAction(Context context) {
        Intent intent = new Intent(context, CollectActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_collect;
    }

    @Override
    protected void initEventAndData() {
        mViewModel.getCollectList(String.valueOf(mCurPage));
    }

    @Override
    protected void onDestroyRelease() {

    }

    protected void initView() {
        mListBeans = new ArrayList<>();
//        collect_include_no_data = findViewById(R.id.collect_include_no_data);
        mCollectRecycle = findViewById(R.id.collect_recycle);
        mCollectRecycle.setLayoutManager(new LinearLayoutManager(this));
        mCollectRecycle.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mCurPage = 1;
                mViewModel.getCollectList(String.valueOf(mCurPage));
            }

            @Override
            public void onLoadMore() {
                mCurPage++;
                mViewModel.getCollectList(String.valueOf(mCurPage));
            }
        });
    }

    private void loading(List<CollectBean.Datas> mList) {
        if (mList != null && mList.size() != 0) {
            mCollectRecycle.loadMoreComplete();
            mListBeans.addAll(mList);
            mCollectAdapter.setData(mListBeans);
            mCollectAdapter.notifyDataSetChanged();
        } else {
            mCollectRecycle.setNoMore(true);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mCollectRecycle.setNoMore(true);
                }
            }, 1000);
        }
    }

    private void refresh(List<CollectBean.Datas> mList) {
        if (mList != null && mList.size() != 0) {
//            collect_include_no_data.setVisibility(View.GONE);
            mCollectRecycle.setVisibility(View.VISIBLE);
            mListBeans.clear();
            mListBeans.addAll(mList);
            if (mCollectAdapter == null) {
                mCollectAdapter = new CollectAdapter(this, mListBeans);
                mCollectRecycle.setAdapter(mCollectAdapter);
                mCollectAdapter.setOnItemClickListener(new CollectAdapter.OnItemClickListener() {
                    @Override
                    public void doCancelCollage(CollectBean.Datas mData, ImageView imageView) {
                        if (ToolUtils.isNull(String.valueOf(mData.getOriginId()))) {
                            mViewModel.doCollectCancel(mData.getId(), -1);
                        } else {
                            mViewModel.doCollectCancel(mData.getId(), mData.getOriginId());
                        }
                    }

                    @Override
                    public void doIntern(CollectBean.Datas mData) {
                        Intent intent = new Intent(CollectActivity.this, CollectDetailActivity.class);
                        intent.putExtra("url", mData.getLink());
                        intent.putExtra("title", mData.getTitle());
                        startActivity(intent);
                    }
                });
            } else {
                mCollectAdapter.setData(mListBeans);
                mCollectAdapter.notifyDataSetChanged();
            }
            mCollectRecycle.refreshComplete();
        } else {
//            collect_include_no_data.setVisibility(View.VISIBLE);
            mCollectRecycle.setVisibility(View.GONE);
        }
    }

    public void getCollectError(String mse) {
        ToastUtil.showShort(this, "登录过期，请重新登录");
        ToolUtils.logout(this);
        LoginActivity.startAction(this);
        finish();
    }

    public void getCollectListSuccess(CollectBean collectBean) {
        mDatas = collectBean.getDatas();
        if (mCurPage == 1) {
            refresh(mDatas);
        } else {
            loading(mDatas);
        }
        ToolUtils.dismissLoading(this);
    }

    public void doCancelCollectSuccessOrError(CommonLiveData commonLiveData) {
        if (CommonLiveData.SUCCESS == commonLiveData.code) {
            mCurPage = 1;
            mViewModel.getCollectList(String.valueOf(mCurPage));
            ToastUtil.showShort(this, "取消收藏成功");
            return;
        }
        ToastUtil.showShort(this, "登录过期，请重新登录");
        ToolUtils.logout(this);
        LoginActivity.startAction(this);
    }

    @Override
    protected void onCreateLoadData() {

    }

    @Override
    protected void onResumeLoadData() {

    }

    @Override
    protected void dataObserver() {
        mViewModel.mDoCancelCollect.observe(this, this::doCancelCollectSuccessOrError);
        mViewModel.mGetCollectList.observe(this, this::getCollectListSuccess);
        mViewModel.mGetCollectListError.observe(this, this::getCollectError);
    }
}
